function $(a) {
  return document.querySelector(a);
}

window.onload = function () {
  var tbody = document.querySelector("tbody");
  // 退出登录
  // $('.exit').onclick=function(){
  //     axios({
  //         method: 'post',
  //         url: '/user/logout',
  //     }).then(function(data){
  //        alert('退出登录')
  //     }).catch(err=>{
  //         console.log(err);
  //     })
  // }

  //  全部数据
  $(".alllist").onclick = function () {
    $(".pageSize").style.display = "block";
    $(".pageSize1").style.display = "none";
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    $("#search").value = "";
    fenye(1, $(".pageSize").value);
  };

  // 点击显示数据每页几条
  $(".pageSize").onchange = function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    fenye(1, $(".pageSize").value);
  };
  $(".pageSize1").onchange = function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    searchdata(1, $(".pageSize1").value);
  };

  // 弹窗显示
  function popup(a, b, c) {
    a.addEventListener("click", function () {
      b.style.display = "block";
      $(".advertisementName").value = "";
      $(".advertisementWeight").value = "";
      $(".file").value = "";
    });
    c.addEventListener("click", function () {
      b.style.display = "none";
    });
  }
  popup($(".add-all"), $(".add-box"), $(".back"));

  // 简介字数太多隐藏
  function str(b) {
    let s = "";
    let length = 20;
    if (b.length >= length) {
      for (let i = 0; i < length; i++) {
        s += b[i];
      }
      s += "...";
    } else {
      for (let i = 0; i < b.length; i++) {
        s += b[i];
      }
    }
    return s;
  }

  function fenye(a, b) {
    axios({
      method: "get",
      url: "/advertisement/findAllAdverLimitPage",
      params: {
        pageNow: a,
        pageSize: b,
      },
    })
      .then(function (data) {
        $("tbody").innerHTML = "";
        $(".pageye").innerHTML = "";
        list(data, fenye);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  fenye(1, 10);
  // 模糊查询
  function searchdata(a, b) {
    var search_input = document.querySelector("#search");
    axios({
      method: "get",
      url: "/advertisement/findAdvertisementByLikeKeyWordsLimitPage",
      params: {
        keyWords: search_input.value,
        pageNow: a,
        pageSize: b,
      },
    })
      .then(function (data) {
        if (data.data.data.data.list.length == 0) {
          $("#search").value = "";
          $(".pageSize1").style.display = "none";
          $(".pageSize").style.display = "";
          $(".nowpage-btn-2").style.display = "none";
          $(".nowpage-btn-1").style.display = "";
          $(
            ".not"
          ).innerHTML = `<i class="fa fa-exclamation-circle" style="color: rgb(255, 187, 0);" aria-hidden="true"></i>抱歉找不到`;
          $(".not").style.display = "";
          setTimeout(() => {
            $(".not").style.display = "none";
          }, 1500);
        } else {
          $("tbody").innerHTML = "";
          $(".pageye").innerHTML = "";
          $(".pageSize1").style.display = "";
          $(".pageSize").style.display = "none";
          $(".nowpage-btn-2").style.display = "";
          $(".nowpage-btn-1").style.display = "none";
          list(data, searchdata);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
  //  点击搜索按钮
  $(".search-btn").addEventListener("click", function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    var search_input = document.querySelector("#search");
    if (search_input.value == "") {
      $(
        ".not"
      ).innerHTML = `<i class="fa fa-exclamation-circle" style="color: rgb(255, 187, 0);" aria-hidden="true"></i>抱歉找不到`;
      $(".not").style.display = "";
      setTimeout(() => {
        $(".not").style.display = "none";
      }, 1500);
    } else {
      $(
        ".not"
      ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>搜索成功`;
      $(".not").style.display = "";
      setTimeout(() => {
        $(".not").style.display = "none";
      }, 1500);
      searchdata(1, $(".pageSize").value);
    }
  });

  //  添加广告
  function addAdvertisement() {
    $(".submit").onclick = function () {
      var file = $(".file").files[0];
      const formdata = new FormData();
      formdata.append("File", file);
      axios({
        url: "/advertisement/addAdvertisement",
        data: formdata,
        params: {
          advertisementName: $(".advertisementName").value,
          advertisementWeight: $(".advertisementWeight").value,
        },
        method: "put",
      })
        .then((data) => {
          $(".not").style.display = "none";
          console.log(10085);
          console.log("addAdvertisement  : ", data.data);
          if (data.data.message == "成功") {
            $(
              ".not"
            ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i> 添加成功`;
            $(".not").style.display = "";
            setTimeout(() => {
              $(".not").style.display = "none";
            }, 1500);
            $(".add-box").style.display = "none";
            fenye($(".nowpage-1").innerHTML, $(".pageSize").value);
          } else {
            $(
              ".not"
            ).innerHTML = `<i class="fa fa-times" aria-hidden="true" style="color:red;"></i> 添加失败`;
            $(".not").style.display = "";
            setTimeout(() => {
              $(".not").style.display = "none";
            }, 1500);
            $(".add-box").style.display = "none";
          }
        })
        .catch((err) => {
          console.log("addAdvertisement err : ", err);
        });
    };
  }
  addAdvertisement();
  // // 分页数据
  function list(data, b) {
    let list = data.data.data.data.list;
    let pageNow = data.data.data.data.pageNow;
    for (let i = 0; i < list.length; i++) {
      let tds = ` <tr>
        <td><input name="checkbox" type="checkbox" value="checkbox" class="chexkbox" /></td>
        <td>${i + 1 + (pageNow - 1) * $(".pageSize").value}</td>
        <td>${
          list[i].advertisementName == null ? "无" : list[i].advertisementName
        }</td>
        <td><a  href='${
          list[i].advertisementPath == null ? "#" : list[i].advertisementPath
        }' style="text-decoration: none; font-weight:600;">${
        list[i].advertisementPath == null ? "无" : "链接"
      }</a></td>
        <td>${
          list[i].advertisementTimeLength == null
            ? "无"
            : list[i].advertisementTimeLength
        }</td>
        <td><button class="modify-btn"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 修改</button><button class='delete-user'><i class="fa fa-trash-o" aria-hidden="true"></i> 删除</button></td>
        
    </tr>`;
      $("tbody").insertAdjacentHTML("beforeend", tds);
      var tbr = tbody.querySelectorAll("tr");
      if (i % 2 == 0) {
        tbr[i].style.backgroundColor = "rgb(220, 220, 220)";
        tbr[i].onmouseover = function () {
          this.style.backgroundColor = "rgb(202, 170, 145)";
        };
        tbr[i].onmouseout = function () {
          this.style.backgroundColor = "rgb(220, 220, 220)";
        };
      }
    }

    // 删除按钮绑定
    var del_btn = document.querySelectorAll(".delete-user");
    for (let i = 0; i < del_btn.length; i++) {
      del_btn[i].onclick = function () {
        var x = 2;
        if ((x = 2)) {
          console.log(x);
          $(".form-pop").querySelector("p").innerHTML = "你确定要删除吗？";
          $(".pop-box").style.display = "block";
          $(".submit-pop1").onclick = function () {
            var advertisementId = list[i].advertisementId;
            console.log(advertisementId);
            delete_advertisement(advertisementId, b);
            $(".pop-box").style.display = "none";
          };
          $(".submit-pop2").onclick = function () {
            $(".pop-box").style.display = "none";
          };
          $(".back-pop").onclick = function () {
            $(".pop-box").style.display = "none";
          };
        }
      };
    }

    //  分页的总量
    var pageTotal = data.data.data.data.pageTotal;
    var pageTotalCount = data.data.data.data.pageTotalCount;
    // 跳转按钮
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    $(".nowpage-btn-2").onclick = function () {
      $(".nowpage-1").innerHTML =
        $(".nowpage").value > pageTotal ? pageTotal : $(".nowpage").value;
      searchdata($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".nowpage-btn-1").onclick = function () {
      $(".nowpage-1").innerHTML =
        $(".nowpage").value > pageTotal ? pageTotal : $(".nowpage").value;
      fenye($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".total").innerHTML = `共${pageTotal}页共${pageTotalCount}条数据`;
    if (pageTotal < 3) {
      for (let i = 0; i < pageTotal; i++) {
        let a = ` <a href="JavaScript:;" class='pagebtn'>${i + 1}</a>`;
        $(".pageye").insertAdjacentHTML("beforeend", a);
      }
    } else {
      for (let i = 0; i < 3; i++) {
        let a = ` <a href="JavaScript:;" class='pagebtn'>${i + 1}</a>`;
        $(".pageye").insertAdjacentHTML("beforeend", a);
      }
      var pagebtnAll = document.querySelectorAll(".pagebtn");
      if ($(".nowpage").value >= 3) {
        let c = $(".nowpage").value;
        let a = c >= pageTotal - 1 ? (c = pageTotal - 1) : c;
        pagebtnAll[0].innerHTML = a - 1;
        pagebtnAll[1].innerHTML = a;
        pagebtnAll[2].innerHTML = ++a;
      }
    }
    var pagebtnAll = document.querySelectorAll(".pagebtn");
    for (let i = 0; i < pagebtnAll.length; i++) {
      pagebtnAll[i].onclick = function () {
        b(this.innerHTML, $(".pageSize").value);
        $(".nowpage-1").innerHTML = `${this.innerHTML}`;
        $(".nowpage").value = $(".nowpage-1").innerHTML;
      };
      if (pagebtnAll[i].innerHTML == $(".nowpage-1").innerHTML) {
        pagebtnAll[i].style.backgroundColor = "rgb(202,170,145)";
      } else if (pagebtnAll[i].innerHTML == $(".nowpage").value) {
        for (let j = 0; j < pagebtnAll.length; j++) {
          pagebtnAll[j].style.backgroundColor = "";
        }
        pagebtnAll[i].style.backgroundColor = "rgb(202,170,145)";
      }
    }
    $("#next").onclick = function () {
      var x = $(".nowpage-1").innerHTML;
      x++;
      var y = x >= pageTotal ? (x = pageTotal) : x;
      $(".nowpage-1").innerHTML = `${y}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b($(".nowpage-1").innerHTML, $(".pageSize").value);
      console.log($(".nowpage-1").innerHTML, $(".nowpage").value);
      console.log($(".nowpage").value);
    };
    $("#pre").onclick = function () {
      var x = $(".nowpage-1").innerHTML;
      x--;
      var y = x <= 1 ? (x = 1) : x;
      $(".nowpage-1").innerHTML = `${y}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".frist").onclick = function () {
      $(".nowpage-1").innerHTML = `1`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b(1, $(".pageSize").value);
    };
    $(".last").onclick = function () {
      $(".nowpage-1").innerHTML = `${pageTotal}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b(pageTotal, $(".pageSize").value);
    };
    // 复选框
    var checkbox = document.querySelectorAll(".chexkbox");
    $("#checkbox-all").onclick = function () {
      if ($("#checkbox-all").checked) {
        for (let i = 0; i < checkbox.length; i++) {
          checkbox[i].checked = "checked";
        }
      } else {
        for (let i = 0; i < checkbox.length; i++) {
          checkbox[i].checked = "";
        }
      }
    };
    $(".delete-all").onclick = function () {
      var x = 3;
      if ((x = 3)) {
        console.log(x);
        $(".form-pop").querySelector("p").innerHTML = "你确定要批量删除吗？";
        $(".pop-box").style.display = "block";
        $(".submit-pop1").onclick = function () {
          for (let i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
              console.log(list[i].advertisementId);
              delete_advertisementAll(list[i].advertisementId);
            }
          }
          alert("删除成功");
          fenye($(".nowpage-1").innerHTML, $(".pageSize").value);
          $(".pop-box").style.display = "none";
        };
        $(".submit-pop2").onclick = function () {
          $(".pop-box").style.display = "none";
        };
        $(".back-pop").onclick = function () {
          $(".pop-box").style.display = "none";
        };
      }
    };

    //  修改按钮绑定
    var modify_btn = document.querySelectorAll(".modify-btn");
    for (let i = 0; i < modify_btn.length; i++) {
      popup(modify_btn[i], $(".modify-box"), $(".back1"));
      modify_btn[i].onclick = function () {
        $(".advertisementName1").value = list[i].advertisementName;
        var advertisementId = list[i].advertisementId;
        console.log(advertisementId);
        updataadvertisementId(advertisementId);
      };
    }
  }

  // 删除单个数据
  function delete_advertisement(advertisementId, b) {
    axios({
      method: "delete",
      url: "/advertisement/deleteAdvertisementById",
      params: {
        advertisementId: advertisementId,
      },
    })
      .then(function (data) {
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
        b($(".nowpage-1").innerHTML, $(".pageSize").value);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  // 批量删除订单
  function delete_advertisementAll(Ids) {
    axios({
      method: "delete",
      url: "/advertisement/deleteAdvertisementByIds",
      params: {
        Ids: Ids,
      },
    })
      .then(function (data) {
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 修改更新数据
  function updataadvertisementId(advertisementId) {
    $(".submit1").onclick = function () {
      var file = $(".file1").files[0];
      const formdata1 = new FormData();
      formdata1.append("File1", file);
      axios({
        method: "put",
        url: "/advertisement/updateAdvertisementMsg",
        data: formdata1,
        params: {
          advertisementName: $(".advertisementName1").value,
          advertisementId: advertisementId,
        },
        method: "put",
      })
        .then(function (data) {
          console.log(data.data.data);
          let data1 = data.data.data;
          if (data1.message == "成功") {
            $(
              ".not"
            ).innerHTML = `<i class="fa fa-check-circle" style="color:green;" aria-hidden="true"></i></i>修改成功`;
            $(".not").style.display = "";
            setTimeout(() => {
              $(".not").style.display = "none";
            }, 2000);
            $(".modify-box").style.display = "none";
            fenye($(".nowpage-1").innerHTML, $(".pageSize").value);
          } else {
            $(
              ".not"
            ).innerHTML = `<i class="fa fa-times" aria-hidden="true" style="color:red;"></i> 修改失败`;
            $(".not").style.display = "";
            setTimeout(() => {
              $(".not").style.display = "none";
            }, 1500);
            $(".modify-box").style.display = "none";
          }
        })
        .catch((err) => {
          console.log(err);
        });
    };
  }
};
